package com.cgb2110.serve.controller;


import com.cgb2110.serve.pojo.Position;
import com.cgb2110.serve.pojo.RespBean;
import com.cgb2110.serve.service.IPositionService;
import com.cgb2110.serve.service.impl.PositionServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Larry
 * @since 2021-12-20
 */
@RestController
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionServiceImpl iPositionService;

    @ApiOperation(value = "获取所有职位信息")
    @GetMapping("/")
    public List<Position> getAllPostions(){
        return iPositionService.list();
    }

    @ApiOperation(value = "添加职位信息")
    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        if (iPositionService.save(position)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "修改职位信息")
    @PutMapping("/")
    public RespBean updatePosition(@RequestBody Position position){

        if (iPositionService.updateById(position)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public RespBean deletePositionById(@PathVariable Integer id){

        if (iPositionService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/")
    public RespBean deletePositionByIds( Integer[] ids){
        List<Integer> idList = Arrays.asList(ids);
        if (iPositionService.removeByIds(idList)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
